import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QAction, QFileDialog, QWidget, QVBoxLayout, \
    QHBoxLayout, QPushButton, QLineEdit
import pytesseract
from PIL import Image


class SuperPlatform(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # 创建文本编辑区域
        self.text_edit = QTextEdit()
        self.setCentralWidget(self.text_edit)

        # 创建菜单栏
        menubar = self.menuBar()
        file_menu = menubar.addMenu('文件')

        # 创建打开文件动作
        open_file_action = QAction('打开', self)
        open_file_action.triggered.connect(self.open_file)
        file_menu.addAction(open_file_action)

        # 创建保存文件动作
        save_file_action = QAction('保存', self)
        save_file_action.triggered.connect(self.save_file)
        file_menu.addAction(save_file_action)

        # 创建退出动作
        exit_action = QAction('退出', self)
        exit_action.triggered.connect(self.close)
        file_menu.addAction(exit_action)

        # 创建计算器菜单
        calculator_menu = menubar.addMenu('计算器')
        calculator_action = QAction('打开计算器', self)
        calculator_action.triggered.connect(self.open_calculator)
        calculator_menu.addAction(calculator_action)

        # 创建图像识别菜单
        ocr_menu = menubar.addMenu('图像识别')
        ocr_action = QAction('进行图像识别', self)
        ocr_action.triggered.connect(self.perform_ocr)
        ocr_menu.addAction(ocr_action)

        # 创建工具栏
        toolbar = self.addToolBar('工具栏')
        toolbar.addAction(open_file_action)
        toolbar.addAction(save_file_action)

        # 设置状态栏
        self.statusBar().showMessage('就绪')

        # 设置窗口属性
        self.setGeometry(300, 300, 800, 600)
        self.setWindowTitle('超级平台')
        self.show()

    def open_file(self):
        file_name, _ = QFileDialog.getOpenFileName(self, '打开文件', '', '文本文件 (*.txt)')
        if file_name:
            try:
                with open(file_name, 'r', encoding='utf-8') as file:
                    content = file.read()
                    self.text_edit.setPlainText(content)
            except Exception as e:
                self.statusBar().showMessage(f'打开文件时出错: {e}')

    def save_file(self):
        file_name, _ = QFileDialog.getSaveFileName(self, '保存文件', '', '文本文件 (*.txt)')
        if file_name:
            try:
                content = self.text_edit.toPlainText()
                with open(file_name, 'w', encoding='utf-8') as file:
                    file.write(content)
                self.statusBar().showMessage('文件保存成功')
            except Exception as e:
                self.statusBar().showMessage(f'保存文件时出错: {e}')

    def open_calculator(self):
        self.calculator = Calculator()
        self.calculator.show()

    def perform_ocr(self):
        file_name, _ = QFileDialog.getOpenFileName(self, '选择图像文件', '', '图像文件 (*.png *.jpg *.jpeg)')
        if file_name:
            try:
                image = Image.open(file_name)
                text = pytesseract.image_to_string(image, lang='chi_sim+eng')
                self.text_edit.setPlainText(text)
                self.statusBar().showMessage('图像识别完成')
            except Exception as e:
                self.statusBar().showMessage(f'图像识别出错: {e}')


class Calculator(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('计算器')
        self.setGeometry(400, 400, 300, 300)

        # 创建布局
        vbox = QVBoxLayout()
        hbox_input = QHBoxLayout()
        hbox_buttons = QHBoxLayout()

        # 创建输入框
        self.input_box = QLineEdit()
        hbox_input.addWidget(self.input_box)

        # 创建按钮
        buttons = [
            '7', '8', '9', '/',
            '4', '5', '6', '*',
            '1', '2', '3', '-',
            '0', '.', '=', '+'
        ]
        for button_text in buttons:
            button = QPushButton(button_text)
            button.clicked.connect(lambda _, txt=button_text: self.on_button_click(txt))
            hbox_buttons.addWidget(button)

        # 布局添加
        vbox.addLayout(hbox_input)
        vbox.addLayout(hbox_buttons)

        self.setLayout(vbox)

    def on_button_click(self, button_text):
        if button_text == '=':
            try:
                result = eval(self.input_box.text())
                self.input_box.setText(str(result))
            except Exception:
                self.input_box.setText('错误')
        else:
            current_text = self.input_box.text()
            self.input_box.setText(current_text + button_text)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    platform = SuperPlatform()
    sys.exit(app.exec_())

